/***************************************************************************
 *   Copyright (C) 2008 by Eduardo Gurgel Pinho                            *
 *   edgurgel@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#ifndef __TRIANGLE_H__
#define __TRIANGLE_H__

#include "object.h"
#include "math/vector.h"
#include "math/ray.h"
#include "material/object_material.h"

#include <boost/tuple/tuple.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/version.hpp>
#include <limits>

using namespace boost::tuples;

namespace Sirat {

namespace Object {
	
class Triangle : public Object {
public:	
	Triangle(Material::Object_Material* _object_material,const Math::Vector&,const Math::Vector&,const Math::Vector&);
	Triangle(Material::Object_Material* _object_material,const Math::Vector* const);
	Triangle();
	Triangle(const Triangle& _triangle);
	~Triangle();
	
	Math::Vector get_vertex(unsigned short&) const;
	virtual Math::Vector get_normal(const Math::Vector&) const;
	
	void set_vertex(const Math::Vector&,unsigned short&);
	
	virtual tuple<double,Math::Vector,Object*> intersection(const Math::Ray&);
	
	virtual tuple<double,double> get_u_v(const Math::Vector& _point) const;
	
	Triangle& operator=(const Triangle& _triangle);

	virtual bool is_limitable() const { return true; }

	virtual Math::Vector get_min_point() const {
		double infinity = std::numeric_limits<double>().infinity();
		Math::Vector result(infinity, infinity, infinity);

		for(int i = 0; i < 3; i++) {
			result.x = std::min(result.x, vertices[i].x);
			result.y = std::min(result.y, vertices[i].y);
			result.z = std::min(result.z, vertices[i].z);
		}

		return result;
	}

	virtual Math::Vector get_max_point() const { 
		double infinity = std::numeric_limits<double>().infinity();
		Math::Vector result(-infinity, -infinity, -infinity);

		for(int i = 0; i < 3; i++) {
			result.x = std::max(result.x, vertices[i].x);
			result.y = std::max(result.y, vertices[i].y);
			result.z = std::max(result.z, vertices[i].z);
		}

		return result;
	}
	
	Object* clone() { return new Triangle(*this); }
private:	
	friend class boost::serialization::access;
	template<class Archive> void serialize(Archive&, const unsigned int);
		
	Math::Vector vertices[3];
	bool cull_face;
};



template<class Archive> void Triangle::serialize(Archive& ar, const unsigned int /* file_version */){
	ar	& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Object)
			& BOOST_SERIALIZATION_NVP(vertices)
			& BOOST_SERIALIZATION_NVP(cull_face);
}

}

}

#endif
